import { Page } from '@/base/Page';
import { getDevcieDistribute } from '@/base/api/DevcieApi';
import { DeviceDistibuteVo } from '@/base/model/DeviceDistibuteVo';
import { getAgentList } from '@/base/api/AgentApi';
import { Agent } from '@/base/model/Agent';
import { Company } from '@/base/model/Company';
import { getCompanyList } from '@/base/api/CompanyApi';
import { Validation } from '@/base/Validation';
import { store } from '@/store/Store';

class Form {
  agentId = 0;
  companyId = 0;
}

export class DeviceBloc {
  form = new Form();
  devicePaged: Page<DeviceDistibuteVo> = new Page<DeviceDistibuteVo>([], 0, 1, 15, 0, []);
  agentOptionsArr: Array<Agent> = [];
  companyIdOptionsArr: Array<Company> = [];
  loading = false;
  seriesData = [
    { name: '北京', value: 0 },
    { name: '天津', value: 0 },
    { name: '上海', value: 0 },
    { name: '重庆', value: 0 },
    { name: '河北', value: 0 },
    { name: '河南', value: 0 },
    { name: '云南', value: 0 },
    { name: '辽宁', value: 0 },
    { name: '黑龙江', value: 0 },
    { name: '湖南', value: 0 },
    { name: '安徽', value: 0 },
    { name: '山东', value: 0 },
    { name: '新疆', value: 0 },
    { name: '江苏', value: 0 },
    { name: '浙江', value: 0 },
    { name: '江西', value: 0 },
    { name: '湖北', value: 0 },
    { name: '广西', value: 0 },
    { name: '甘肃', value: 0 },
    { name: '山西', value: 0 },
    { name: '内蒙古', value: 0 },
    { name: '陕西', value: 0 },
    { name: '吉林', value: 0 },
    { name: '福建', value: 0 },
    { name: '贵州', value: 0 },
    { name: '广东', value: 0 },
    { name: '青海', value: 0 },
    { name: '西藏', value: 0 },
    { name: '四川', value: 0 },
    { name: '宁夏', value: 0 },
    { name: '海南', value: 0 },
    { name: '台湾', value: 0 },
    { name: '香港', value: 0 },
    { name: '澳门', value: 0 }
  ];
  myChartChina: any;
  optionMap: any;

  load() {
    // 判断用户是否是品牌商登录
    if (Validation.isRootGeneral) {
      this.form.companyId = store.state.user.company.company.id
    }
    this.devcieDistribute();
    this.companyLoad('');
    this.agentLoad('');
  }

  searchLoad() {
    this.devcieDistribute();
  }

  devcieDistribute() {
    this.loading = true;
    getDevcieDistribute({
      pageIndex: this.devicePaged.pageIndex,
      pageSize: this.devicePaged.pageSize,
      sortDirection: 'ASC',
      agentId: this.form.agentId === 0 ? [] : [this.form.agentId],
      companyId: this.form.companyId === 0 ? [] : [this.form.companyId]
    }).then((res) => {
      this.devicePaged = res.data;
      this.seriesData.forEach(x => {
        const item = this.devicePaged.items.find(j => j.province.includes(x.name));
        item ? x.value = item.sum : x.value = 0
      });
      this.myChartChina.setOption(this.optionMap);
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }

  companyLoad(val) {
    getCompanyList({
      pageIndex: 1,
      pageSize: 50,
      sortDirection: 'DESC',
      name: val
    }).then((res) => {
      this.companyIdOptionsArr = res.data.items;
    })
  }

  changeSelect(val) {
    this.form.companyId = val;
    this.agentLoad('');
    this.form.agentId = 0;
  }

  agentLoad(val) {
    getAgentList({
      pageIndex: 1,
      pageSize: 50,
      agentName: val,
      companyId: this.form.companyId === 0 ? [] : [this.form.companyId]
    }).then(res => {
      this.agentOptionsArr = res.data.items;
    });
  }

  reset() {
    this.form = new Form();
    this.devicePaged.pageIndex = 1;
    this.devicePaged.pageSize = 15;
    this.load();
  }

}
